<template>
  <view class="login-container">
    <view class="top-image">
    	<image mode="widthFix" src="../../static/img/logo.jpg"></image>
    </view>
    <view class="login-form-container">
      <u-form :model="form" ref="uForm">
        <u-form-item label="账号">
          <u-input v-model="username" />
        </u-form-item>
        <u-form-item label="密码">
          <u-input
            v-model="password"
            type="password"
          />
        </u-form-item>
        <u-form-item>
          <u-button
            type="primary"
            @click="policeLogin"
          >
            登录
          </u-button>
        </u-form-item>
      </u-form>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        username: '',
        password: '',
        rePassWord: ''
      };
    },
    mounted () {
      this.init()
    },
    methods: {
      init () {
        // 页面pv事件埋点
        this.$aplus.aplus_queue.push({
          'action':'aplus.sendPV',
          'arguments':[{
            is_auto: false
          }, {
            sapp_id:'19344',//isv根据实际填写自己应用的参数
            sapp_name:'zhongdianchelian',//isv根据实际填写自己应用的参数
            // 自定义PV参数key-value键值对，以下内容必填，根据应用实际情况设置。
            page_id: 'login',//isv根据实际填写自己应用的参数
            page_name: '登录',//isv根据实际填写自己应用的参数
            page_url: 'pages/login/login'//isv根据实际填写自己应用的参数
          }]
        })
        uni.getStorage({
          key: 'username',
          success: (res) => {
            this.username = res.data
          }
        })
        uni.getStorage({
          key: 'password',
          success: (res) => {
            this.password = res.data
          }
        })
      },
      checkPassWord(PassWord){
        if(PassWord==''){
          uni.showToast({
            title:'密码不能为空',
            icon:'none'
          })
          return false;
        }
        return true
      },
      checkUser(username){
        if(!username){
          uni.showToast({
            title:'用户名不能为空',
            icon:'none'
          })
          return false
        }
        return true
      },
      // 交警登录
      async policeLogin(){
        const hasUsername = this.checkUser(this.username)
        if(!hasUsername) return 
        const hasPassWord = this.checkPassWord(this.password)
        if(!hasPassWord) return 
        let msg = '登录中'
        const loadingComponent = uni.showLoading({
          title:msg,
          mask:true
        })
        const policeForm = {
          username:this.username,
          password:this.password,
          sys: 2
          // sys:this.$config.sys
        }
        try{
          const logRes = await this.$http.post(`Account/login`,policeForm,{
            'content-type': 'application/x-www-form-urlencoded'
          })
          uni.hideLoading()
          uni.showToast({
            title: '登录成功',
            icon: 'none',
            duration:2000
          })
          const token = logRes.result.token
          uni.setStorage({
            key: 'token',
            data: token,
            success: () => {
              uni.setStorage({
                key: 'password',
                data: this.password,
                success: () => {
                  uni.setStorage({
                    key: 'username',
                    data: this.username,
                    success: () => {
                      uni.redirectTo({
                        url: '../../pages/index/index'
                      })
                    }
                  })
                }
              })
            }
          })
        }catch(err){
          const msg = err.result || err
          uni.hideLoading()
          uni.showToast({
            title: '登录失败,'+msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      toLogin () {
        this.$http.get('Muck/GetStaffList',{
          Token: this.token
        }).then(res=>{
          // this.roleDriverList = res.result.List
          if(isStateList){
            this.roleDriverListTemp = [...res.result.List]
            this.SET_state({roleDriverList:res.result.List})
          }else{
            this.roleDriverListTemp = [...res.result.List]
          }
          this.addShowList()
          uni.showToast({
            title:res.result.Count + '条信息'
          })
        }).catch(err=>{
          const msg = err.result || err
          uni.showToast({
            title:'获取数据失败,'+msg,
            icon:'none'
          })
        })
      }
    }
  }
</script>

<style lang="scss">
.login-container{
  display: flex;
  justify-content: center;
  flex-direction: column;
  .top-image{
    text-align: center;
  }
  .login-form-container{
    padding: 0rpx 32rpx;
  }
}
</style>
